const mysql = require('../../utils/db').mysql;
const reConnect = require('../../utils/db').reConnect;
const md5 = require('md5');
const session = require('../../utils/userStatus');
exports.page = function(req, res, next){
    res.render('admin/login');
};
exports.getLogin = function(req, res, next){
    const passwd = md5(req.body.passwd);
    const queryStr = 'SELECT * FROM adminuser WHERE username="' +　req.body.username + '"';
    mysql.getConnection(function(err, conn) {
        conn.query(queryStr, function (err, result) {
            if (err) {
                reConnect();
                res.send({
                    msg: 'error'
                });
            }
            if (result[0]) {
                if (result[0].passwd == passwd) {
                    let userAllInfo = result[0];
                    userAllInfo = Object.assign({}, userAllInfo, {
                        userName: result[0].username,
                        userType: result[0].usertype,
                        userId: result[0].id
                    });
                    session.setUserStatus(req, userAllInfo);
                    res.send({
                        code: 200,
                        msg: '登录成功'
                    });
                } else {
                    res.send({
                        code: 401,
                        msg: '密码错误'
                    });
                }
            } else {
                res.send({
                    code: 400,
                    msg: '登录失败，没有该用户'
                });
            }
            mysql.releaseConnection(conn);
        });
    })
}